package com.cldz.service;

import com.cldz.common.dto.req.DevAlarmDto;
import com.cldz.common.dto.res.DeviceAlarmResDto;
import com.cldz.entity.DeviceAlarm;
import com.cldz.dao.DevAlarmMapper;

import com.cldz.utils.BeanCopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ Author     ：lixiao.
 * @ Date       ：Created in 13:14 2018/12/15
 * @ Description：
 * @ Modified By：
 */
@Service("devAlarmService")
public class DevAlarmService {

    @Autowired
    private DevAlarmMapper devAlarmMapper;



    public List<DeviceAlarmResDto> getRunAlarmData(DevAlarmDto devAlarmDto){
        List<DeviceAlarm> alarms = devAlarmMapper.getRunAlarmData(devAlarmDto);
        return copyListBean(alarms);
    }

    public Integer getRunAlarmDataAmount(DevAlarmDto devAlarmDto){
        Integer amount = devAlarmMapper.getRunAlarmDataAmount(devAlarmDto);

        return amount;
    }

    public List<DeviceAlarmResDto> getRunDeviceAlarmData(DevAlarmDto devAlarmDto) {
        List<DeviceAlarm> alarms = devAlarmMapper.getRunDeviceAlarmData(devAlarmDto);
        return copyListBean(alarms);
    }

    public Integer getRunDeviceAlarmDataAmount(DevAlarmDto devAlarmDto){
        return devAlarmMapper.getRunDeviceAlarmDataAmount(devAlarmDto);
    }

    public List<DeviceAlarmResDto> getHisAlarmData(DevAlarmDto devAlarmDto) {

        List<DeviceAlarm> alarms = devAlarmMapper.getHisAlarmData(devAlarmDto);
        return copyListBean(alarms);
    }

    public Integer getHisAlarmDataAmount(DevAlarmDto devAlarmDto) {
        return devAlarmMapper.getHisAlarmDataAmount(devAlarmDto);
    }

    public List<DeviceAlarmResDto> getHisDeviceAlarmData(DevAlarmDto devAlarmDto) {
        List<DeviceAlarm> alarms = devAlarmMapper.getHisDeviceAlarmData(devAlarmDto);
        return copyListBean(alarms);
    }

    public Integer getHisDeviceAlarmDataAmount(DevAlarmDto devAlarmDto) {
        return devAlarmMapper.getHisDeviceAlarmDataAmount(devAlarmDto);
    }

    public List<Map<String, Object>> getDevsRunAlarmCount(String[] devs) {
        List<Map<String,Object>> list = new ArrayList<>();
        for (String dev : devs ) {
            Integer count = devAlarmMapper.getDevsRunAlarmCount(dev);
            Map<String,Object> map = new HashMap<>();
            map.put("deviceID", Integer.parseInt(dev));
            map.put("count",count);
            list.add(map);
        }
        return list;
    }


    public List<DeviceAlarmResDto> getAlarmMsgs(DevAlarmDto devAlarmDto){
        List<DeviceAlarm> alarms = devAlarmMapper.getAlarmMsgs(devAlarmDto);
        return copyListBean(alarms);
    }

    public Integer getAlarmMsgsAmount(DevAlarmDto devAlarmDto){
        return devAlarmMapper.getAlarmMsgsAmount(devAlarmDto);
    }

    private List<DeviceAlarmResDto> copyListBean(List<DeviceAlarm> devListByTypeID) {
        List<DeviceAlarmResDto> list = new ArrayList<>();
        for (DeviceAlarm device : devListByTypeID) {
            DeviceAlarmResDto dto = new DeviceAlarmResDto();
            BeanCopyUtil.copyPropertiesIgnoreNull(device, dto);
//            BeanUtils.copyProperties(device, dto);
            list.add(dto);
        }
        return list;
    }



}
